package com.jf.docsify.modules.system.user;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.jf.docsify.common.exception.DocsifyException;
import com.jf.docsify.common.utils.AssertUtil;
import com.jf.docsify.common.utils.DbTxUtil;
import com.jf.docsify.modules.model.SysAuthRegister;
import com.jf.docsify.modules.model.SysUser;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;

/**
 * Description: 授权注册
 *
 * @author chenwr
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本           修改人       修改日期         修改内容
 * 2022/8/2.1       chenwr    2022/8/2           Create
 * </pre>
 * @date 2022/8/2
 */
public class AuthRegisterService {

    SysAuthRegister dao = new SysAuthRegister().dao();

    @Inject
    UserService userService;

    public boolean addAuthRegisterUser(SysAuthRegister sysAuthRegister) {
        String userName = sysAuthRegister.getUserName();
        AssertUtil.hasText(userName, "授权注册用户名不能为空");
        List<SysUser> users = userService.findByUserName(userName);
        if (CollectionUtils.isNotEmpty(users)) {
            DocsifyException.throwE("该用户名已存在");
        }
        Long expireTime = sysAuthRegister.getExpireTime();
        if (expireTime == null) {
            long time = new Date().getTime();
            time = time + (3 * 24 * 60 * 60 * 1000);
            sysAuthRegister.setExpireTime(time);
        }
        return sysAuthRegister.save();
    }

    public void deleteByUserName(String userName) {
        AssertUtil.hasText(userName, "用户名不能为空");
        SysAuthRegister sysAuthRegister = new SysAuthRegister();
        List<SysAuthRegister> sysAuthRegisters = sysAuthRegister.find("select * from sys_auth_register where user_name = ?", userName);
        delete(sysAuthRegisters);
    }

    public List<SysAuthRegister> authRegister(String userName) {
        AssertUtil.hasText(userName, "用户名不能为空");
        SysAuthRegister sysAuthRegister = new SysAuthRegister();
        return sysAuthRegister.find("select * from sys_auth_register where user_name = ?", userName);
    }

    public boolean delete(List<SysAuthRegister> sysAuthRegisters) {
        if (CollectionUtils.isNotEmpty(sysAuthRegisters)) {
            return DbTxUtil.tx(() -> sysAuthRegisters.stream().forEach(register -> register.delete()));
        }
        DocsifyException.throwE("删除失败");
        return false;
    }

    public boolean validateHasRegisterAuth(List<SysAuthRegister> sysAuthRegisters) {
        if (CollectionUtils.isEmpty(sysAuthRegisters)) {
            DocsifyException.throwE("无注册权限");
        }
        long time = new Date().getTime();
        for (int i = 0; i < sysAuthRegisters.size(); i++) {
            SysAuthRegister sysAuthRegister = sysAuthRegisters.get(i);
            Long expireTime = sysAuthRegister.getExpireTime();
            if (expireTime != null && expireTime.compareTo(time) > 0) {
                return true;
            }
        }
        delete(sysAuthRegisters);
        return false;
    }

    public Page<SysAuthRegister> list(String currentPage, String pageSize) {
        return dao.paginate(Integer.valueOf(currentPage), Integer.valueOf(pageSize), "select * ", "from sys_auth_register");
    }

    public boolean deleteById(String id) {
        SysAuthRegister sysAuthRegister = new SysAuthRegister();
        sysAuthRegister.setId(Integer.valueOf(id));
        return sysAuthRegister.delete();
    }
}